package org.yourorghere;

import com.sun.opengl.util.GLUT;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLJPanel;
import javax.media.opengl.glu.GLU;


public class CarGL  implements GLEventListener, Runnable {

    public CarGL(int width, int height, GLJPanel panel) 
    {
        fAspect = (float) width / height;
        terrain = new Terrain();
        terrain.load("ground.txt");
        camera = new Camera();
        this.panel = panel;
    }

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();

        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(60, fAspect, 0.5, 500);


        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
        glu.gluLookAt(0,20, 100, 0, 0, 0, 0, 1, 0);


    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();

        if (height <= 0) { // avoid a divide by zero error!

            height = 1;
        }
        fAspect = (float) width / height;
        gl.glViewport(0, 0, width, height); // Set our viewport to the size of our window  
        gl.glMatrixMode(GL.GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed  
        gl.glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
        glu.gluPerspective(60, fAspect, 0.5, 500.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes  
        gl.glMatrixMode(GL.GL_MODELVIEW);

    }

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        GLUT glut = new GLUT();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glColor3f(0.0f, 1.0f, 0.0f);

        camera.placeCamera(drawable);
        gl.glPushMatrix();
        terrain.draw(drawable);
        gl.glPopMatrix();

        gl.glColor3f(1.0f, 0.0f, 0.0f);

       
         //BEGIN AXIS============================
        gl.glColor3f(1.0f, 1.0f, 0.0f);

        gl.glBegin(GL.GL_LINES);
        gl.glVertex3f(0.0f, 0.0f, 0.0f);
        gl.glVertex3f(20.0f, 0.0f, 0.0f);
        gl.glEnd();

        gl.glColor3f(0.0f, 0.0f, 1.0f);

        gl.glBegin(GL.GL_LINES);
        gl.glVertex3f(0.0f, 0.0f, 0.0f);
        gl.glVertex3f(0.0f, 20.0f, 0.0f);
        gl.glEnd();


        gl.glColor3f(1.0f, 1.0f, 1.0f);

        gl.glBegin(GL.GL_LINES);
        gl.glVertex3f(0.0f, 0.0f, 0.0f);
        gl.glVertex3f(0.0f, 0.0f, 20.0f);
        gl.glEnd();
        //END AXIS =============================
        
        
       


        //terrain.draw(drawable);
        
        gl.glFlush();
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) 
    {
        
    }
    

    public void run() 
    {
        while (true) {
	    try {
                camera.update();
                this.panel.repaint();
		Thread.sleep(fps);
	    }
	    catch (InterruptedException e) {
		return;
	    }
	}
    }
    
    private float fAspect = 1.0f;
    private Terrain terrain;
    private Camera camera;
    private GLJPanel panel;
    private long fps = 100;
}
